package com.example.demo.scheduledtask;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.TransferOutListVo;
import com.example.demo.vo.TransferOutPostVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class TransferOutPost {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;

    /**
     * 二阶调拨拨出过账
     */
//    @Scheduled(cron = "50 0/2 * * * ?")//开启定时同步
    @Scheduled(fixedDelay = 35 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void TransferOutPost() {
        //查询没有回调的数据
        TransferOutPostVo transferOutPostVo = wmsPurchaseMapper.getTransferOutPost();
        synchronized (transferOutPostVo) {
//        WarehouseTransferPostVo warehouseTransferPost = new WarehouseTransferPostVo();
            if (ObjectUtils.isEmpty(transferOutPostVo) || ObjectUtil.isEmpty(transferOutPostVo.getIndcdocno())) {
                log.info("未查询到二阶调拨拨出过账数据，修改报错数据重新过账");
                wmsPurchaseMapper.TransferOutPostLose();
                return;
            }

            try {
                transferOutPostVo.setIndcsite(Constant.PMDSSITE_AMA);
                transferOutPostVo.setActionType(Constant.ActionType_I);
                List<TransferOutListVo> transferOutListVos = null;
                //当有不同的批次时传递实际扫描的批次
                Integer count = wmsPurchaseMapper.getTransferOutListCount(transferOutPostVo.getIndcdocno());
                if (count > 0) {
                    transferOutListVos = wmsPurchaseMapper.getTransferOutListCallback(transferOutPostVo.getIndcdocno());
                }

                String resMessage = soapXmlUtil.getSoapXml("TransferOutPost", "indc_t", "indd_t", transferOutPostVo, transferOutListVos);

                //转换为soap消息

                log.info("soap二阶调拨出库过账请求报文->{}", resMessage);


                //回写状态修改为 erp未回填
                wmsPurchaseMapper.updateTransferOutPost(transferOutPostVo.getIndcdocno(), "erp待返回", 3);
                //发送soap消息，并解析结果
                String result;
                try {
                    result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
                } catch (Exception e) {
                    result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
                }
                //调用等待
//            if (transferOutListVos.size() >= 50) {
//                Thread.sleep(100 * 1000);
//            } else if (transferOutListVos.size() >= 30) {
//                Thread.sleep(60 * 1000);
//            } else if (transferOutListVos.size() >= 10) {
//                Thread.sleep(30 * 1000);
//            } else if (transferOutListVos.size() >= 5) {
//                Thread.sleep(10 * 1000);
//            }


                log.info("soap二阶调拨出库过账回调返回报文->{}", result);
                XmlResp xmlResp = soapXmlUtil.analysisXml(result);
                if (xmlResp.getCode().equals("0")) {
                    //更新相应表状态为已下发
                    wmsPurchaseMapper.updateTransferOutPost(transferOutPostVo.getIndcdocno(), xmlResp.getData().get("indcdocno"), 1);
                } else {
                    //更新相应表状态为已下发
                    wmsPurchaseMapper.updateTransferOutPost(transferOutPostVo.getIndcdocno(), xmlResp.getMessage(), 2);
                }
            } catch (Exception e) {
                wmsPurchaseMapper.updateTransferOutPost(transferOutPostVo.getIndcdocno(), "非常见的数据处理问题，请联系开发人员处理", 2);
                e.printStackTrace();
            }
        }
    }
}
